<%@page import="cn.sendto.model.UserInfoModel"%>
<%@page import="cn.sendto.service.UserInfoService"%>
<%@page import="cn.sendto.model.ResidentInfoModel"%>
<%@page import="java.util.ArrayList"%>
<%@page import="cn.sendto.service.ResidentInfoService"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" errorPage="err.jsp"%>
    <jsp:include page="lock.jsp"></jsp:include>
<%
	//设置编码方式
	request.setCharacterEncoding("UTF-8");
	//获取用户类别
	String rank = request.getParameter("rank");
	
	ArrayList<Integer> errlist = new ArrayList<Integer>();
	//班级
	if(rank.equals("1")){
		String username = request.getParameter("username");
		String name = request.getParameter("name");
		String gender = request.getParameter("gender");
		String age = request.getParameter("age");
		String tellphone = request.getParameter("tellphone");
		String idcard = request.getParameter("idcard");
		
		//判断数据有效性
		if(name == null || name.equals("")){
			errlist.add(1);
		}
		if(age == null || age.equals("")){
			errlist.add(3);
		}
		if(tellphone == null || tellphone.equals("")){
			errlist.add(4);
		}
		if(idcard == null || idcard.equals("")){
			errlist.add(5);
		}
		//有空数据返回
		if(errlist.size() > 0){
			request.setAttribute("errinfo", errlist);
			request.getRequestDispatcher("alterinfo.jsp").forward(request, response);
			return;
		}
		//修改信息
		ResidentInfoService ris = new ResidentInfoService();
		int i = ris.alterinfo(name,gender,age,tellphone,idcard,username);
		//返回值大于0说明修改成功
		if(i > 0 ){
			request.setAttribute("alter", "修改成功");
			ResidentInfoModel rim = new ResidentInfoModel();
			rim = (ResidentInfoModel) session.getAttribute("loginResident");
			rim.setName(name);
			rim.setTellphone(tellphone);
			session.removeAttribute("loginResident");
			session.setAttribute("loginResident", rim);
			request.getRequestDispatcher("alterinfo.jsp").forward(request, response);
			return;
		}else{
			request.setAttribute("alter", "修改失败");
			request.getRequestDispatcher("alterinfo.jsp").forward(request, response);
			return;
		}
		//管理员
	}else if(rank.equals("2")){
		//接收数据
		String username = request.getParameter("username");
		String name = request.getParameter("name");
		String usertel = request.getParameter("usertel");
		//判断数据有效性
		if(name == null || name.equals("")){
			errlist.add(1);
		}
		if(usertel == null || usertel.equals("")){
			errlist.add(2);
		}
		//有无效数据返回
		if(errlist.size() > 0){
			request.setAttribute("errinfo", errlist);
			request.getRequestDispatcher("alterinfo.jsp").forward(request, response);
			return;
		}
		
		//修改信息
		UserInfoService uis = new UserInfoService();
		int i = uis.alterinfo(name,usertel,username);
		//修改成功
		if(i > 0 ){
			request.setAttribute("alter", "修改成功");
			UserInfoModel uim = new UserInfoModel();
			//存储信息
			uim = (UserInfoModel) session.getAttribute("loginUser");
			uim.setName(name);
			uim.setUsertel(usertel);
			//删除之前登陆状态
			session.removeAttribute("loginUser");
			//存储新的状态
			session.setAttribute("loginUser", uim);
			//跳转			
			request.getRequestDispatcher("alterinfo.jsp").forward(request, response);
			return;
		}else{
			request.setAttribute("alter", "修改失败");
			request.getRequestDispatcher("alterinfo.jsp").forward(request, response);
			return;
		}
	}
	
%>